<?php

namespace app\api\controller;
use think\Db;
class Yuyue extends Base {

    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            echo json_encode(['status'=>301,'msg'=>"Token失效！请重新登录！"]);
            exit;
        }
    }
    /**
     * 预约首页
     */
    public  function yuyueIndex(){
        extract(input());
        $type = Db::table('up_u_user')->where('user_id',$user_id)->value('user_type');
        $num = Db::table('up_yuyue')->where('yy_user_id',$user_id)->where('yy_del',1)->count();
        $pic = Db::table('up_lunbo')->where('lunbo_type',2)->find();
        $guize = Db::table('up_recovery')->where('rec_status',1)->select();
        $self = array();
        //计算时的价格依据
        $day = Db::table('up_set')->where('id',1)->find();
        $unit_price = $day['price'];
        if($type == 2){
            $self = Db::table('up_daili_rec')->where('status',1)->select();
            $unit_price = $day['price']+$day['butie'];
        }

        return json_encode(['status'=>200,'pic'=>$pic,'num'=>$num,'guize'=>$guize,'self'=>$self,'unit_price'=>$unit_price]);
    }


    /**
     * 新增预约订单：用户和运营端通用
     * @return bool
     */
    public  function addYuyue(){
        extract(input());
        $number = date('YmdHis') . rand(1000000, 9999999);
        $userInfo = Db::table('up_u_user')
            ->field('user_type, user_shi')
            ->where('user_id',$user_id)
            ->find();
        $day = Db::table('up_set')->where('id',1)->find();
        $data = array();
        $data['yy_number'] = $number;
        $data['yy_type'] = $userInfo['user_type'];
        $data['yy_user_id'] = $user_id;
        $data['yy_weight'] = $weight;
        $data['yy_time'] = $time;
        $data['yy_createtime'] = date('Y-m-d H:i:s');
        $data['yy_price'] = $day['price'];
        if($userInfo['user_type'] == 2){
            $data['yy_butie'] = $day['butie'];
        }
        if(isset($content)){
            $data['yy_content'] = $content;
        }
        $res = Db::table('up_yuyue')->insert($data);
        if($res){
            if($userInfo['user_type'] == 1){//用户预约需要推送
                $user_yunying = Db::name('u_user')
                    ->where('user_shi', $userInfo['user_shi'])
                    ->where('user_status', 1)
                    ->where('user_can_yuyue', 1)
                    ->where('user_type', 2)
                    ->value('user_id');
                if($user_yunying){
                    try{
                        $yunying_id = (string)$user_yunying;
                        $str = '您收到一个订单号为' . $number . '的预约订单';
                        $this->Jpush(1, $yunying_id, $str, ['type' => '']);
                    }catch(\Exception $e){
        
                    }
                }
            }
            return json_encode(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json_encode(['status'=>3,'msg'=>'提交失败']);
        }
    }
    /**
     * 我的预约订单：用户和运营端通用
     * type:不传或传0或空，page
     * @return bool
     */
    public  function myYuyue(){
        extract(input());
        $where = array();
        $where['yy_user_id'] = $user_id;
        $where['yy_del'] = 1;
        if(isset($type) && $type){
            $where['yy_status'] = $type ;
        }
        $data = Db::table('up_yuyue')
            ->where($where)
            ->order('yy_status ASC,yy_time DESC')
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 我收到的订单：运营端使用
     * type:不传或传0或空，page
     * @return bool
     */
    public  function myReceiveYuyue(){
        extract(input());
        $can = Db::table('up_u_user')->where('user_id',$user_id)->value('user_can_yuyue');
        if($can == 1){
            $shi = Db::table('up_u_user')->where('user_id',$user_id)->value('user_shi');
            $uids = Db::table('up_u_user')
                ->where('user_shi',$shi)
                ->where('user_type',1)
                ->column('user_id');
            $data = array();
            if($uids){
                $where = array();
                $where['yy_user_id'] = array('in',$uids);
//                $where['yy_del'] = 1;
                if(isset($type) && $type){
                    $where['yy_status'] = $type ;
                }
                $data = Db::table('up_yuyue')
                    ->where($where)
                    ->order('yy_status ASC,yy_time DESC')
                    ->page($page,10)
                    ->select();
            }
            return json_encode(['status'=>200,'msg'=>$data]);
        }else{
            $data = array();
            return json_encode(['status'=>200,'msg'=>$data]);
        }

    }
    /**
     * 预约订单详情：用户和运营端通用
     * @return bool
     */
    public  function myYuyueInfo(){
        extract(input());
        $where = array();
        $where['yy_id'] = $yyid;
        $data = Db::table('up_yuyue')->alias('y')
            ->join('up_u_user u','y.yy_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('y.*,user_id,user_phone,user_address,user_ssq_str')
            ->find();
        if($data['yy_admin_image']){
            $data['yy_admin_image'] = explode(',',$data['yy_admin_image']);
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 用户或运行端：更改订单状态
     * yyid，status，image（只在运行端确认时上传凭证）
     * @return bool
     */
    public  function yuyueStatus(){
        extract(input());
        $where = array();
        $where['yy_id'] = $yyid;
        $user_type = Db::table('up_u_user')->where('user_id',$user_id)->value('user_type');
        $yy_type = Db::table('up_yuyue')->where($where)->value('yy_type');
        $yy_status = Db::table('up_yuyue')->where($where)->value('yy_status');
        $update = array();
        $update['yy_status'] = $status;
        //针对用户端：已确认是运行端来确认的
        if($status == 2){
            if($user_type == 1 && $yy_type == 1){
                return json_encode(['status'=>0,'msg'=>'您不能确认']);
            }
            if($user_type == 2 && $yy_type == 2){
                if(!isset($image) || !$image){
                    return json_encode(['status'=>0,'msg'=>'您不能确认']);
                }else{
                    //运行端确认时上传凭证
                    $update['yy_admin_image'] = trim($image,',');
                }
            }
        }
        //针对运行端：已完成是后台来确认的
        if($status == 3){
            if($user_type == 2 && $yy_type == 2){
                return json_encode(['status'=>0,'msg'=>'您不能确认']);
            }
        }
        //针对取消：已完成是不能取消的（PS:只有我自己的才可以取消，我收到的预约没有取消的功能）
        if($status == 4){
            if($yy_status == 3){
                return json_encode(['status'=>0,'msg'=>'已完成的不能取消']);
            }
        }
        $update['yy_update'] = date('Y-m-d H:i:s');
        $res = Db::table('up_yuyue')->where($where)->update($update);
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>3,'msg'=>'失败']);
        }
    }
    /**
     * 用户或运行端：删除订单
     * yyid
     * @return bool
     */
    public  function yuyueDel(){
        extract(input());
        $where = array();
        $where['yy_id'] = $yyid;
        $update = array();
        $update['yy_del'] = 2;
        $res = Db::table('up_yuyue')->where($where)->update($update);
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>3,'msg'=>'失败']);
        }
    }

}